﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections;

namespace MM.Common.Base
{
    public static class Character
    {
        // Methods
        public static string CompartStr()
        {
            return "|";
        }

        public static string ConvertDbcToSbc(this string str)
        {
            char[] chars = str.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                byte[] bytes = Encoding.Unicode.GetBytes(chars, i, 1);
                if ((bytes.Length == 2) && (bytes[1] == 0))
                {
                    bytes[0] = (byte)(bytes[0] - 0x20);
                    bytes[1] = 0xff;
                    chars[i] = Encoding.Unicode.GetChars(bytes)[0];
                }
            }
            return new string(chars);
        }

        public static string ConvertSbcToDbc(this string str)
        {
            char[] chars = str.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                byte[] bytes = Encoding.Unicode.GetBytes(chars, i, 1);
                if ((bytes.Length == 2) && (bytes[1] == 0xff))
                {
                    bytes[0] = (byte)(bytes[0] + 0x20);
                    bytes[1] = 0;
                    chars[i] = Encoding.Unicode.GetChars(bytes)[0];
                }
            }
            return new string(chars);
        }

        private static bool FindKeyInArray(string[] strs, string key)
        {
            if (strs == null)
            {
                return false;
            }
            int index = 0;
            index = 0;
            while (index < strs.Length)
            {
                if (strs[index] == key)
                {
                    break;
                }
                index++;
            }
            return (index < strs.Length);
        }

        public static void GetAgeInfo(this string ageandunit, ref int age, ref string ageunit)
        {
            age = 0;
            ageunit = "Y";
            try
            {
                string str = "";
                if (ageandunit.Length >= 2)
                {
                    str = ageandunit.Substring(ageandunit.Length - 1, 1);
                }
                string str2 = str;
                if (str2 == null)
                {
                    goto Label_00B7;
                }
                if (!(str2 == "天"))
                {
                    if (str2 == "月")
                    {
                        goto Label_0079;
                    }
                    if (str2 == "岁")
                    {
                        goto Label_0098;
                    }
                    goto Label_00B7;
                }
                ageunit = "D";
                age = int.Parse(ageandunit.Substring(0, ageandunit.Length - 1));
                return;
            Label_0079:
                ageunit = "M";
                age = int.Parse(ageandunit.Substring(0, ageandunit.Length - 1));
                return;
            Label_0098:
                ageunit = "Y";
                age = int.Parse(ageandunit.Substring(0, ageandunit.Length - 1));
                return;
            Label_00B7:
                age = int.Parse(ageandunit.Substring(0, ageandunit.Length));
            }
            catch
            {
            }
        }

        public static ArrayList GetListFromString(this string source_str, string split_str)
        {
            ArrayList list = new ArrayList();
            int startIndex = 0;
            while (startIndex < source_str.Length)
            {
                int index = source_str.IndexOf(split_str, startIndex);
                if (index >= 0)
                {
                    string item = source_str.Substring(startIndex, index - startIndex);
                    if (!list.Contains(item))
                    {
                        list.Add(item);
                    }
                    startIndex += index + 1;
                }
                else
                {
                    list.Add(source_str.Substring(startIndex));
                    return list;
                }
            }
            return list;
        }

        public static int GetStringASCIILength(this string orastr)
        {
            int length = orastr.Length;
            char[] chArray = orastr.ToCharArray();
            for (int i = 0; i < chArray.Length; i++)
            {
                if (Convert.ToInt32(chArray[i]) > 0xff)
                {
                    length++;
                }
            }
            return length;
        }

        public static string GetSubStringASCII(this string orastr, int len)
        {
            char[] chArray = orastr.ToCharArray();
            int index = 0;
            index = 0;
            while ((index < chArray.Length) && (len > 0))
            {
                if (Convert.ToInt32(chArray[index]) > 0xff)
                {
                    len--;
                }
                index++;
                len--;
            }
            if (len < 0)
            {
                index--;
            }
            return orastr.Substring(0, index);
        }

        public static bool IsDateTime(this string strValue)
        {
            if (strValue != null)
            {
                string pattern = @"[1-2]{1}[0-9]{3}((-|\/){1}(([0]?[1-9]{1})|(1[0-2]{1}))((-|\/){1}((([0]?[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1})))( (([0-1]{1}[0-9]{1})|2[0-3]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{3})?)?)?)?$";
                Regex regex = new Regex(pattern);
                if (regex.IsMatch(strValue))
                {
                    int length = -1;
                    int index = -1;
                    int num3 = -1;
                    if (-1 != (length = strValue.IndexOf("-")))
                    {
                        index = strValue.IndexOf("-", (int)(length + 1));
                        num3 = strValue.IndexOf(":");
                    }
                    else
                    {
                        length = strValue.IndexOf("/");
                        index = strValue.IndexOf("/", (int)(length + 1));
                        num3 = strValue.IndexOf(":");
                    }
                    if (-1 == index)
                    {
                        return true;
                    }
                    if (-1 == num3)
                    {
                        num3 = strValue.Length + 3;
                    }
                    int num4 = Convert.ToInt32(strValue.Substring(0, length));
                    int num5 = Convert.ToInt32(strValue.Substring(length + 1, (index - length) - 1));
                    int num6 = Convert.ToInt32(strValue.Substring(index + 1, (num3 - index) - 4));
                    if (((num5 < 8) && (1 == (num5 % 2))) || ((num5 > 8) && ((num5 % 2) == 0)))
                    {
                        if (num6 < 0x20)
                        {
                            return true;
                        }
                    }
                    else if (num5 != 2)
                    {
                        if (num6 < 0x1f)
                        {
                            return true;
                        }
                    }
                    else if (((num4 % 400) == 0) || (((num4 % 4) == 0) && (0 < (num4 % 100))))
                    {
                        if (num6 < 30)
                        {
                            return true;
                        }
                    }
                    else if (num6 < 0x1d)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public static bool IsInt(this string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            return Regex.IsMatch(value, "^(0|[1-9][0-9]*)$");
        }

        public static bool IsNumeric(this string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            return Regex.IsMatch(value, @"^\d+(\.\d+)?$");
        }

        public static bool IsUnsign(this string value)
        {
            return Regex.IsMatch(value, @"^[1-9]\d*$");
        }

        public static int NumStringToNumArray(string numString, int unitLength, string BefortFixText, string LastFixText, out List<string> numArray, out string rtnText)
        {
            string[] strArray = new string[500];
            int num = NumStringToNumArray(numString, unitLength, BefortFixText, LastFixText, out strArray, out rtnText);
            numArray = new List<string>();
            for (int i = 0; i < num; i++)
            {
                numArray.Add(strArray[i]);
            }
            return num;
        }

        public static int NumStringToNumArray(string numString, int unitLength, string BefortFixText, string LastFixText, out string[] numArray, out string rtnText)
        {
            rtnText = "";
            if (string.IsNullOrEmpty(numString))
            {
                numArray = new string[0];
                return 0;
            }
            numString = numString.Replace("—", "-");
            numString = numString.Replace("_", "-");
            numString = numString.Replace("。", ".");
            numString = numString.Replace("，", ",");
            string[,] strArray = new string[0x3e8, 2];
            int num = 0;
            int num2 = 0;
            int length = numString.Length;
            for (int i = 0; i < length; i++)
            {
                if (numString[i] == ',')
                {
                    num++;
                    num2 = 0;
                }
                else if (numString[i] == '-')
                {
                    num2 = 1;
                }
                else if (char.IsNumber(numString[i]))
                {
                    string[,] strArray3;
                    IntPtr ptr;
                    IntPtr ptr2;
                    (strArray3 = strArray)[(int)(ptr = (IntPtr)num), (int)(ptr2 = (IntPtr)num2)] = strArray3[(int)ptr, (int)ptr2] + numString[i];
                }
                else
                {
                    rtnText = "数值字符串中存在字母<" + numString[i] + ">";
                    break;
                }
            }
            string[] strs = new string[0x3e8];
            int index = 0;
            int num6 = 0;
            int num7 = 0;
            int num8 = 0;
            for (int j = 0; j <= num; j++)
            {
                if (strArray[j, 0] != "")
                {
                    if ((strArray[j, 1] == null) || (strArray[j, 1] == ""))
                    {
                        if (!FindKeyInArray(strs, strArray[j, 0]))
                        {
                            strs[index] = strArray[j, 0];
                            index++;
                        }
                    }
                    else
                    {
                        num6 = Convert.ToInt16(strArray[j, 0]);
                        num7 = Convert.ToInt16(strArray[j, 1]);
                        if (num6 > num7)
                        {
                            num8 = num6;
                            num6 = num7;
                            num7 = num8;
                        }
                        for (int n = num6; n <= num7; n++)
                        {
                            if (!FindKeyInArray(strs, n.ToString()))
                            {
                                strs[index] = n.ToString();
                                if (index >= 0x3e8)
                                {
                                    MessageBox.Show("标本范围之中的数量不能多于1000个.");
                                    numArray = null;
                                    return -1;
                                }
                                index++;
                            }
                        }
                    }
                }
            }
            for (int k = 0; k < index; k++)
            {
                if ((strs[k] != null) && (strs[k] != ""))
                {
                    strs[k] = BefortFixText + strs[k].PadLeft(unitLength, '0') + LastFixText;
                }
            }
            numArray = new string[index];
            for (int m = 0; m < index; m++)
            {
                numArray[m] = strs[m];
            }
            return index;
        }

        public static string ReplaceInBracket(this string source_str, string replace_str)
        {
            return source_str.ReplaceInBracket(replace_str, '(', ')');
        }

        public static string ReplaceInBracket(this string source_str, string replace_str, char start_char, char end_char)
        {
            int index = source_str.IndexOf(start_char);
            int startIndex = source_str.IndexOf(end_char);
            if ((index != -1) && (startIndex != -1))
            {
                return (source_str.Substring(0, index + 1) + replace_str + source_str.Substring(startIndex, source_str.Length - startIndex));
            }
            return source_str;
        }

        public static string UpperMoney(decimal money)
        {
            string str = "零壹贰叁肆伍陆柒捌玖";
            string str2 = "十亿仟佰拾万仟佰拾元角分";
            money = decimal.Round(money, 2) * 100.00M;
            if (money == 0.00M)
            {
                return "";
            }
            string str5 = money.ToString();
            int index = str5.IndexOf('.');
            if (index > 0)
            {
                str5 = str5.Substring(0, index);
            }
            int length = str5.Length;
            string str6 = "整";
            index = str2.Length - 1;
            string str4 = "";
            string str3 = "";
            int startIndex = length - 1;
            while (startIndex >= 0)
            {
                string str7 = str5.Substring(startIndex, 1);
                if ("9876543210 ".IndexOf(str7) < 0)
                {
                    return str6;
                }
                int num3 = int.Parse(str7);
                str3 = str.Substring(num3, 1);
                str4 = str2.Substring(index, 1);
                if (str7 == "0")
                {
                    if ("元,万,亿".IndexOf(str4) >= 0)
                    {
                        str3 = "";
                    }
                    else if ((startIndex < (length - 1)) && (str5.Substring(startIndex, 2) == "00"))
                    {
                        str4 = "";
                        str3 = "";
                    }
                    else
                    {
                        str4 = "";
                        if (startIndex == (length - 1))
                        {
                            str3 = "";
                        }
                    }
                }
                str6 = str3 + str4 + str6;
                startIndex--;
                index--;
            }
            return str6;
        }

        public static string WipeLastSeparator(this string source_str)
        {
            return source_str.WipeLastSeparator(',');
        }

        public static string WipeLastSeparator(this string source_str, char wipe_char)
        {
            int length = source_str.LastIndexOf(wipe_char);
            if (length > 0)
            {
                source_str = source_str.Substring(0, length);
            }
            return source_str;
        }

        public static string WriteXml(string[] element, string[] content)
        {
            using (StringWriter writer = new StringWriter())
            {
                XmlTextWriter writer2 = new XmlTextWriter(writer);
                writer2.WriteStartDocument();
                writer2.WriteStartElement("root");
                for (int i = 0; i < element.Length; i++)
                {
                    writer2.WriteStartElement(element[i]);
                    writer2.WriteString(content[i]);
                    writer2.WriteEndElement();
                }
                writer2.WriteEndElement();
                writer2.WriteEndDocument();
                return writer.ToString();
            }
        }
    }
}
